#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File   : txUdpServ.py
# @Email  : xuexia_xu@feidee.com
# @Time   : 2024/4/7 11:10
# @Desc   : 《Python核心编程第3版》2.4.6 示例 2-6 UDP 时间戳服务器
from socket import *
from time import ctime

HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
# 创建服务器套接字
udpSerSock = socket(AF_INET, SOCK_DGRAM)
# 套接字与地址绑定
udpSerSock.bind(ADDR)

while True:
    print("waiting for message...")
    # 对话（接收/发送）
    data, addr = udpSerSock.recvfrom(BUFSIZ)
    # print('11...received from and returned to:', addr)
    udpSerSock.sendto(b'[%s] %s' % (bytes(ctime(), 'utf-8'), data), addr)
    print('...received from and returned to:', addr)

# 关闭服务器套接字#（可选）
udpSerSock.close()
